from sqlalchemy_search import *


class Search_system:
    def __init__(self):
        self.input = input_achieve
        self.read = read_achieve
        self.registor = registor_user
        self.login = user_login
        self.login_error = {}

    def registry(self, name, passwd, role):
        roleid = None
        if role == "教师":
            roleid = 1
        if role == "学生":
            roleid = 2
        if not roleid:
            print("角色有误，请重新输入！")
            return
        try:
            if self.registor(name, passwd, roleid):
                print("~~~~~~~~~~~~注册成功！~~~~~~~~~~~~~")
        except ValueError:
            print("该用户名已经存在！")

    def user_login(self, name, passwd):
        if self.login_error.get(name) == 3:
            print('用户锁定，无法登录')
            return

        success = self.login(name, passwd)
        print(success)
        if success is None:
            print("该用户不存在！")
        elif success == 0:
            self.login_error[name] = self.login_error.get(name, 0) + 1
            print("密码错误！")
        else:
            print("~~~~~~~~~~~~登录成功！~~~~~~~~~~~~~")
            return success

    def writer(self, st_name, chinese, math, english):
        self.input(st_name, chinese, math, english)

    def reader(self, name):
        return self.read(name)
    #
    # def close(self):
    #     self.database.close()


search_input = Search_system()


if __name__ == '__main__':
    while True:
        try:
            regis_or_login = int(input("准备注册还是登录>>>:\n1.注册;\n2.登录;\n3.退出;\n请输入序号："))
        except ValueError:
            continue

        if regis_or_login == 1:
            input_name = input("注册用户名>>>:")
            input_passwd = input("密码>>>:")
            input_role = input("角色（学生或教师）>>>:")
            search_input.registry(input_name, input_passwd, input_role)

        elif regis_or_login == 2:
            while True:
                input_name = input("登录用户名>>>:")
                input_passwd = input("密码>>>:")
                role = search_input.user_login(input_name, input_passwd)

                if role == 1:
                    while True:
                        try:
                            index = int(input("输入还是查询的成绩>>>:\n1.输入;\n2.查询;\n3.退出;\n请输入序号："))
                        except ValueError:
                            index = 999
                        if int(index) == 1:
                            student_name = input("输入学生姓名：")
                            chinese = input("语文成绩：")
                            math = input("数学成绩：")
                            english = input("英语成绩：")
                            search_input.writer(student_name, chinese, math, english)
                        elif int(index) == 2:
                            student_name = input("输入查询成绩的学生姓名：")
                            res = search_input.reader(student_name)
                            if res:
                                print("语文:{}, 数学:{}, 英语:{}".format(res["chinese"], res["math"], res["english"]))
                            else:
                                print("你输入的学生还没有成绩！")
                        elif int(index) == 3:
                            break
                        else:
                            print("输入有误，请重新输入！")

                elif role == 2:
                    while True:
                        try:
                            index = int(input("输入还是查询的成绩>>>:\n1.查询;\n2.退出;\n请输入序号："))
                        except ValueError:
                            index = 999
                        if int(index) == 1:
                            student_name = input("输入查询成绩的学生姓名：")
                            res = search_input.reader(student_name)
                            if res:
                                print("语文:{}, 数学:{}, 英语:{}".format(res["chinese"], res["math"], res["english"]))
                            else:
                                print("你输入的学生还没有成绩！")
                        elif int(index) == 2:
                            break
                        else:
                            print("输入有误，请重新输入！")

        elif regis_or_login == 3:
            break
